package me.yushuai.wukong.base.controller;

import me.yushuai.wukong.base.api.HsCodeApi;
import me.yushuai.wukong.base.api.dto.CreateHsCodeDTO;
import me.yushuai.wukong.base.api.dto.HsCodeDTO;
import me.yushuai.wukong.base.api.dto.PagingSearchHsCode;
import me.yushuai.wukong.base.api.dto.UpdateHsCodeDTO;
import me.yushuai.wukong.base.api.vo.HsCodeVO;
import me.yushuai.wukong.base.entity.HsCode;
import me.yushuai.wukong.base.service.HsCodeService;
import me.yushuai.wukong.common.dto.PageDTO;
import me.yushuai.wukong.common.error.WuKongException;
import me.yushuai.wukong.common.mvc.BaseController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 海关编码控制器
 *
 * @author zhoushuai@189.cn
 * @since 2022-08-25
 */
@RestController
public class HsCodeController extends BaseController implements HsCodeApi {

    /**
     * 海关编码业务类
     */
    private final HsCodeService hsCodeService;

    @Autowired
    public HsCodeController(HsCodeService hsCodeService) {
        this.hsCodeService = hsCodeService;
    }

    @Override
    public Long createHsCode(CreateHsCodeDTO createHsCodeDTO) throws WuKongException {
        return this.hsCodeService.createHsCode(createHsCodeDTO);
    }

    @Override
    public Long editHsCode(Long hsCodeId, UpdateHsCodeDTO updateHsCodeDTO) throws WuKongException {
        updateHsCodeDTO.setHsCodeId(hsCodeId);
        return this.hsCodeService.updateHsCode(updateHsCodeDTO);
    }

    @Override
    public Long deleteHsCode(Long hsCodeId) throws WuKongException {
        this.hsCodeService.deleteHsCode(hsCodeId);
        return hsCodeId;
    }

    @Override
    public HsCodeVO getById(Long hsCodeId) throws WuKongException {
        final HsCodeDTO hsCode = this.hsCodeService.findOne(hsCodeId);
        final HsCodeVO result = new HsCodeVO();
        BeanUtils.copyProperties(hsCode, result);
        return result;
    }

    @Override
    public PageDTO<HsCodeVO> pagingSearchHsCode(PagingSearchHsCode page) {

        final Specification<HsCode> spec = (root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();

            // 2. if the HsCode not empty and add the HsCode's condition.
            final String hsCode = page.getHsCode();
            if (StringUtils.hasText(hsCode)) {
                list.add(criteriaBuilder.equal(root.get(HsCode.HS_CODE).as(String.class), hsCode));
            }

            final Predicate[] predicates = list.toArray(new Predicate[0]);
            return criteriaBuilder.and(predicates);
        };

        PageDTO<HsCodeDTO> hsCodePage = this.hsCodeService.pagingSearchHsCode(page, spec);
        List<HsCodeVO> hsCodeList = hsCodePage.getData().stream().map(v -> {
            final HsCodeVO vo = new HsCodeVO();
            BeanUtils.copyProperties(v, vo);
            return vo;
        }).collect(Collectors.toList());
        return PageDTO.of(page, hsCodeList);
    }
}
